package com.lidx.back.statistics.controller.admin;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.lidx.back.statistics.commons.MaterialAction;
import com.lidx.back.statistics.entity.MaterialCategory;
import com.lidx.back.statistics.entity.MaterialRecord;
import com.lidx.back.statistics.service.IMaterialCategoryService;
import com.lidx.back.statistics.service.IMaterialRecordService;

@Controller
@RequestMapping(value="/admin/material")
public class MaterialController {

	@Autowired
	private IMaterialCategoryService categoryService;
	@Autowired
	private IMaterialRecordService recordService;
	
	@RequestMapping(value="list",method=RequestMethod.GET)
	public String MaterialRecords(){
		return "material/list";
	}
	
	@RequestMapping(value="list",method=RequestMethod.POST)
	public String MaterialRecordsPost(Model model,@RequestParam(value="materialRecords") String action,
			@RequestParam String versionType,
			@RequestParam(required=false) String from,
			@RequestParam(required=false) String to){
		if(StringUtils.isNotBlank(action) && StringUtils.isNotBlank(versionType)){
			model.addAttribute("action", action);
			model.addAttribute("versionType", versionType);
			if(StringUtils.isNotBlank(from)&&StringUtils.isNotBlank(to)){
				model.addAttribute("from", from);
				model.addAttribute("to", to);
			}
			//if(StringUtils.isNoneBlank(to)&& StringUtils.isNoneBlank(from)){
				MaterialRecord record = new MaterialRecord();
				List<Object[]> list = recordService.getRecordsWithParams(action,versionType,from,to);
				model.addAttribute("records", list);
			//}
		}
		
		
		return "material/list";
	}
	
	@RequestMapping(value="categoryList",method=RequestMethod.GET)
	public String categoryManage(Model model){
		List<MaterialCategory> categories = categoryService.getCategories(null);
		if(categories != null){
			model.addAttribute("categories", categories);
		}
		return "materialCategory/list";
	}
	
	@RequestMapping(value="addCategory",method=RequestMethod.GET)
	public String addCategory(){
		return "material/addCategory";
	}
	
	@RequestMapping(value="addCategory",method=RequestMethod.POST)
	public String addCategoryPost(@RequestParam String categoryName,@RequestParam String order,
			@RequestParam String gender){
		if(StringUtils.isEmpty(categoryName)){
			return "redirect:/material/addCategory";
		}
		try{
			MaterialCategory category = new MaterialCategory();
			category.setCategoryName(categoryName);
			category.setGender(gender);
			category.setOrder(order);
			categoryService.saveMaterialCategory(category);
		}catch(Exception e){
			e.printStackTrace();
			return "redirect:/material/addCategory";
		}
		
		return "material/addCategory";
	}
}
